package com.ceilcell.wx.controller;

import com.ceilcell.wx.model.ImageMessageXML;
import com.ceilcell.wx.model.TextMessageXML;
import com.ceilcell.wx.util.CheckUtil;
import com.ceilcell.wx.util.ImageNode;
import com.ceilcell.wx.util.MessageUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/5.
 */

@RestController
public class WXInitialController {

    @RequestMapping(value= "/wx", method = RequestMethod.GET)
    public String wxget(@RequestParam(value="signature", defaultValue="signature") String signature,
                     @RequestParam(value="timestamp", defaultValue="timestamp") String timestamp,
                     @RequestParam(value="nonce", defaultValue="nonce") String nonce,
                     @RequestParam(value="echostr", defaultValue="echostr") String echostr,
                     @RequestParam(value="token", defaultValue="token") String token){

        if (CheckUtil.checkSignature(signature, timestamp, nonce)) {
            System.out.println(echostr);

            return echostr; // 校验通过，原样返回echostr参数内容
        }else{
            return "hello, this is a test" + " " + signature + " " + timestamp + " " + nonce + " " + echostr + " " + token;
        }
    }

    @RequestMapping(value="/wx", method = RequestMethod.POST)
    public void wxpost(HttpServletRequest request, HttpServletResponse response){

        PrintWriter out = null;
        TextMessageXML text = new TextMessageXML();
        ImageMessageXML image = new ImageMessageXML();

        try {
            request.setCharacterEncoding("utf-8");
            response.setContentType("text/xml;charset=utf-8");

            out = response.getWriter();

            Map<String, String> map = MessageUtil.xmlToMap(request);
            String toUserName = map.get("ToUserName");
            String fromUserName = map.get("FromUserName");
            String msgType = map.get("MsgType");

            String message = null;
            if ("text".equals(msgType)) {                // 对文本消息进行处理
                String content = map.get("Content");
                text.setFromUserName(toUserName);         // 发送和回复是反向的
                text.setToUserName(fromUserName);
                text.setMsgType("text");
                text.setCreateTime(new Date().getTime());
                text.setContent("你发送的消息是：" + content);
                message = MessageUtil.textMessageToXML(text);
                System.out.println(message);
            }else if("image".equals(msgType)){

                String mediaId = map.get("MediaId");

                image.setCreateTime(new Date().getTime());
                image.setFromUserName(toUserName);
                image.setToUserName(fromUserName);
                image.setMsgType("image");

                ImageNode imagenode = new ImageNode();
                imagenode.setMediaId(mediaId);

                image.setImage(imagenode);

                message = MessageUtil.imageMessageToXML(image);
                System.out.println(message);
            }

            out.print(message);                        // 将回应发送给微信服务器
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
